package pers.myz.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import pers.myz.common.QueryPageParam;
import pers.myz.common.Result;
import pers.myz.entity.SnsBlogComment;
import pers.myz.entity.SnsFriend;
import pers.myz.entity.SnsFriendEventJoin;
import pers.myz.service.SnsBlogCommentService;
import pers.myz.service.SnsFriendEventJoinService;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzw
 * @since 2023-07-01
 */
@RestController
@RequestMapping("/sns-friend-event-join")
public class SnsFriendEventJoinController {

    @Autowired
    private SnsFriendEventJoinService snsFriendEventJoinService;

    @GetMapping("/ifJoin")
    public Result ifFriend(@RequestParam Integer eventId,@RequestParam Integer joinId){
        List list = snsFriendEventJoinService.lambdaQuery().eq(SnsFriendEventJoin::getEventId,eventId).eq(SnsFriendEventJoin::getJoinId,joinId).list();
        if(list.size()>0){
            SnsFriendEventJoin join1 = (SnsFriendEventJoin)list.get(0);
            HashMap res = new HashMap();
            res.put("join",join1);
            return Result.suc(res);
        }else{
            return Result.fail();
        }
    }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody SnsFriendEventJoin friendEventJoin){
        return snsFriendEventJoinService.save(friendEventJoin)?Result.suc():Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String eventId = (String)param.get("eventId");

        Page<SnsFriendEventJoin> page = new Page();
        page.setCurrent(query.getJoinNum());
        page.setSize(query.getJoinSize());

        LambdaQueryWrapper<SnsFriendEventJoin> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.orderByDesc(SnsFriendEventJoin::getCreateTime);
        if(StringUtils.isNotBlank(eventId) && !"null".equals(eventId)){
            lambdaQueryWrapper.eq(SnsFriendEventJoin::getEventId,eventId);
        }
        IPage result = snsFriendEventJoinService.pageC(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }

}
